package com.ce.webframework.sec.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.ce.webframework.sec.dao.ISecUserinfoDao;
import com.ce.webframework.sec.model.SecMenuitemDO;
import com.ce.webframework.sec.model.SecUserinfoDO;
import com.sitechasia.webx.core.dao.hibernate3.HibernateDomainObjectDao;
import com.sitechasia.webx.core.support.Page;

public class SecUserinfoDaoImpl extends HibernateDomainObjectDao<SecUserinfoDO> implements ISecUserinfoDao {

	public SecUserinfoDO getSecUser(String usercode, String password){

		System.out.println("SecUserinfoDao username=" + usercode + ",password=" + password);

		String hql = "FROM SecUserinfoDO info WHERE info.usercode=? and info.password=?";
		Query query = getSession().createQuery(hql);
		query.setParameter(0, usercode);
		query.setParameter(1, password);
		List userList = query.list();
		if (userList == null || userList.size() == 0) {
			System.out.println("is't null");
			return null;
		}
		System.out.println("dao not null");
		return (SecUserinfoDO) userList.get(0);

	}

	/**
	 * 得到分页用户信息
	 */
	@SuppressWarnings("unchecked")
	public Page getSecUsers(String userCode, String userName, String email, int pageNo, int pageSize,
			String sortStr) {
		logger.info("------excute getUsers method------");
		List params = new ArrayList();
		StringBuffer hql = new StringBuffer("FROM SecUserinfoDO info where 1=1 ");
		if (userCode != null && !"".equals(userCode)) {
			params.add(userCode);
			hql.append("AND info.usercode = ?");
		}
		if (userName != null && !"".equals(userName)) {
			params.add(userName);
			hql.append("AND info.username = ?");
		}

		if (email != null && !"".equals(email)) {
			params.add(email);
			hql.append("AND info.email = ?");
		}

		if (sortStr != null && !"".equals(sortStr)) {
			hql.append(" order by " + sortStr);
		} else {
			hql.append(" order by info.userId desc ");
		}
		return this.pagedQuery(hql.toString(), pageNo, pageSize, params.toArray());
	}

	public void addSecUser(SecUserinfoDO userinfoDO){

		getSession().persist(userinfoDO);
		System.out.println("persist successful");
	}

	public SecUserinfoDO findById(Long id){
		String hql = "FROM SecUserinfoDO info WHERE info.userId=? ";
		Query query = getSession().createQuery(hql);
		query.setParameter(0, id);
		List userList = query.list();
		if(userList.size() == 1){
			return (SecUserinfoDO)userList.get(0);
		}
		return null;
	}

	public void updateSecUser(SecUserinfoDO userinfoDO){
//		System.out.println("in update--->"+userinfoDO.getUserId());
		this.getSession().update(userinfoDO);
		System.out.println("merge successful");
	}

	/**
	 * 得到分页用户信息
	 */
	@SuppressWarnings("unchecked")
	public Page getUsers(String userCode, String userName, String sex, int pageNo, int pageSize,
			String sortStr) {
		logger.info("------excute getUsers method------");
		List params = new ArrayList();
//		UserExtention user = (UserExtention) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		StringBuffer hql = new StringBuffer("from SecUserinfoDO u where 1=1 ");
		if (userCode != null && !"".equals(userCode)) {
			params.add('%'+userCode+'%');
			hql.append("and usercode like ?");
		}
		if (userName != null && !"".equals(userName)) {
			params.add('%'+userName+'%');
			hql.append("and username like ?");
		}
		if (sex != null && !"".equals(sex) && !"2".equals(sex)) {
			Boolean boolGender = new Boolean(true);
			if ("1".equals(sex))
				boolGender = new Boolean(true);
			if ("0".equals(sex))
				boolGender = new Boolean(false);
			params.add(boolGender);
			hql.append("and sex = ?");
		}

//		hql.append("and corpid = ?");
//		params.add(user.getCorpid());
		if (sortStr != null && !"".equals(sortStr)) {
			hql.append(" order by " + sortStr);
		}
//		System.out.println("hql--->"+hql);
		return this.pagedQuery(hql.toString(), pageNo, pageSize, params.toArray());
	}

	public void deleteSecUser(String[] delList){
		String str = "";
		for(int m = 1; m < delList.length + 1; m++){
			str = str + ",?" + m;
		}
		str = str.replaceFirst(",","");
		String eql = "DELETE FROM SecUserinfoDO info WHERE info.userId IN ("+str+")";
//		System.out.println("eql------>"+eql);
		try{

			this.executeUpdate(eql,delList);

		}catch(Exception e){
			e.printStackTrace();
		}

	}

	/**
	 * 执行批量插入，更新，删除等操作
	 */
	public void executeUpdate(String updateStr, String[] params) {
		try {
			System.out
					.println("DAO.executeUpdate.updateStr: ===\n" + updateStr);

			Query query = getSession().createQuery(updateStr);
			for (int i = 0; i < params.length; i++) {
				query.setParameter("" + (i + 1), params[i]);
			}
			query.executeUpdate();
		} catch (RuntimeException re) {
			re.printStackTrace();
			// log.error("executeUpdate failed", re);
			throw re;
		}
	}

	public List<SecMenuitemDO> getMenuItemList(List<String> menuCodes, Long epId){
		List<SecMenuitemDO> userList = new ArrayList<SecMenuitemDO>();
//		for(String code : menuCodes){
			String hql = " SELECT info FROM SecMenuitemDO info WHERE info.functionEntityId in (:menuCodes) ";
			Query query = getSession().createQuery(hql);
			query.setParameterList("menuCodes", menuCodes);
//			List code = query.list();
//			if(query)
			userList = query.list();
//		}
//		System.out.println("userList--->"+userList);
		return userList;
	}	
}
